package com.shut.vehicle.controller;

import com.shut.vehicle.controller.result.Result;
import com.shut.vehicle.interceptor.token.Back;
import com.shut.vehicle.interceptor.token.Client;
import com.shut.vehicle.interceptor.token.UserThread;
import com.shut.vehicle.pojo.CarNum;
import com.shut.vehicle.service.CardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 卢斌
 */
@ResponseBody
@RequestMapping(value = "/card")
@Controller
public class CardInfoController {

    @Autowired
    private CardInfoService cardInfoService;


    @Client
    @PostMapping(value = "/release")
    public Result addCardInfo(@RequestParam("file") MultipartFile file) {
        Integer userId = UserThread.userId();
        cardInfoService.addCardInfo(file, userId);
        return Result.ok();
    }


    @Client
    @GetMapping(value = "/card_list")
    public Result listSelfCard(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Integer userId = UserThread.userId();
        List<CarNum> carNums = cardInfoService.getCarNum(userId, pageNum, pageSize);
        return Result.okList(carNums);
    }


    @Back
    @GetMapping(value = "/card_list_target")
    public Result listUserCard(@RequestParam("userId") Integer userId, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        List<CarNum> carNums = cardInfoService.getCarNum(userId, pageNum, pageSize);
        return Result.okList(carNums);
    }


    @Client
    @DeleteMapping(value = "/remove_card")
    public Result removeCardInfo(@RequestBody Integer cardId) {
        Integer userId = UserThread.userId();
        cardInfoService.deleteCardInfo(cardId, userId);
        return Result.ok();
    }

}
